<? $errMsg =''?>
<?

$date_added=date("Y-m-d H:i:s");
if (isset($_POST['btnQuery'])) {
	if($year_box=$_POST['year'])
	{
		echo $year_box;
	}
	else
	{
		@$result = mysql_query("update tbl_order set tinh_trang='0',last_modified='$date_added' where id='".$id."'",$conn);
	}
	
}

?>
<form method="POST" name="frmForm" action="./">
<input type="hidden" name="act" value="report">

<?
$sql="select * from tbl_product";
$result=mysql_query($sql,$conn);
$num_rows_product = mysql_num_rows($result);

$sql="select * from tbl_order";
$result=mysql_query($sql,$conn);
$num_rows_order = mysql_num_rows($result);

$sql="select * from tbl_order where tinh_trang='1'";
$result=mysql_query($sql,$conn);
$order_money_com= mysql_fetch_row($result);
$num_rows_order_com = mysql_num_rows($result);

$sql="select SUM(tong_tien) from tbl_order";
$result=mysql_query($sql,$conn);
$order_money= mysql_fetch_array($result);

$sql="select SUM(tong_tien) from tbl_order where tinh_trang='1'";
$result=mysql_query($sql,$conn);
$order_money_com= mysql_fetch_row($result);
$year=date("Y");
?>
<table width="100%" border="0" cellpadding="2" cellspacing="0">
<tr>
  <td width="83%" class="smallfont"><table width="100%" border="0" cellpadding="2" cellspacing="0">
    <tr>
      <td class="3" height="10"></td>
    </tr>
    <tr>
      <td width="16%" class="smallfont" align="right">Tổng số sản phẩm</td>
      <td width="0%" class="smallfont" align="center"></td>
      <td width="83%" class="smallfont"><b>
        <?=$num_rows_product?>
      </b></td>
    </tr>
    <tr>
      <td width="16%" class="smallfont" align="right">Tổng số đơn đặt hàng</td>
      <td width="0%" class="smallfont" align="center"></td>
      <td width="83%" class="smallfont"><b>
        <?=$num_rows_order?>
      </b></td>
    </tr>
    <tr>
      <td width="16%" class="smallfont" align="right">Tổng số đơn đặt hàng xong</td>
      <td width="0%" class="smallfont" align="center"></td>
      <td width="83%" class="smallfont"><b>
        <?=$num_rows_order_com?>
      </b></td>
    </tr>
    <tr>
      <td width="16%" class="smallfont" align="right">Tổng tiền (VNĐ)</td>
      <td width="0%" class="smallfont" align="center"></td>
      <td width="83%" class="smallfont"><b>
        <?=number_format($order_money[0]*1000) ?>
      </b></td>
    </tr>
    <tr>
      <td width="16%" align="right" class="smallfont">Tổng tiền đơn hoàn thành</td>
      <td width="0%" rowspan="2" align="center" class="smallfont"></td>
      <td width="83%" class="smallfont"><b>
        <?=number_format($order_money_com[0]*1000)?>
      </b></td>
    </tr>
   
    <tr>
      <td class="smallfont" align="right">&nbsp;</td>
      <td class="smallfont" align="center"></td>
      <td width="1%" class="smallfont" >&nbsp;</td>
    </tr>
    <tr>
      <td width="16%" rowspan="2" align="right" class="smallfont">Chọn năm tra cứu</td>
      <td width="0%" rowspan="2" align="center" class="smallfont"></td>
      <td width="83%" class="smallfont" ><select name="year">
<option value=<?=$year-1?> ><?=$year-1?> </option>
<option value=<?=$year?>  selected="selected" ><?= $year?></option>
<option value=<?=$year+1?>><?= $year+1 ?></option>
</select></td>
    </tr>
    <tr>
      <td class="smallfont" ><input type="submit" value="Tra cứu" name="btnQuery" class="button" id="btnQuery" /></td>
    </tr>
    <tr>
      <td class="3" height="10"></td>
    </tr>
  </table>    <b></b></td>
</tr>
</table>
<table border="1" cellpadding="2" bordercolor="#C9C9C9" width="100%">
	<tr>
		<th width="201" class="title">Tháng</th>
		<th width="161" class="title">Tổng đơn hàng</th>
		<th width="201" class="title">Tổng tiền</th>
		<th width="180" class="title">Tổng đơn hàng hoàn thành</th>
		<th width="238" class="title">Tổng tiền đơn hàng hoàn thành</th>    
	</tr>
  
<?php
function tong_don_hang($con,$year)
{
$tong_don_hang_array=array();
$thang=1;
for($thang;$thang<13;$thang++)
{
$sql ="SELECT * FROM tbl_order WHERE DATE_FORMAT(date_added,'%Y-%c')='$year-$thang'";
$result=mysql_query($sql,$con);
$num_rows_order = mysql_num_rows($result);
$tong_don_hang_array[$thang]=$num_rows_order;
}

return $tong_don_hang_array;
}
function tong_tien($con,$year)
{
$tong_tien_array=array();
$thang=1;
for($thang;$thang<13;$thang++)
{
$sql ="SELECT SUM(tong_tien) FROM tbl_order WHERE DATE_FORMAT(date_added,'%Y-%c')='$year-$thang'";
$result=mysql_query($sql,$con);
$tong=mysql_fetch_row($result);
$tong_tien_array[$thang]=$tong[0];
}
return $tong_tien_array;
}
function tong_don_hang_hoan_thanh($con,$year)
{
	$tong_don_hang_array_com=array();
$thang=1;
for($thang;$thang<13;$thang++)
{
$sql ="SELECT * FROM tbl_order WHERE DATE_FORMAT(date_added,'%Y-%c')='$year-$thang' AND tinh_trang='1'";
$result=mysql_query($sql,$con);
$num_rows_order = mysql_num_rows($result);
$tong_don_hang_array_com[$thang]=$num_rows_order;
}

return $tong_don_hang_array_com;
	}
function tong_tien_hoan_thanh($con,$year)
{
$tong_tien_array_com=array();
$thang=1;
for($thang;$thang<13;$thang++)
{
$sql ="SELECT SUM(tong_tien) FROM tbl_order WHERE DATE_FORMAT(date_added,'%Y-%c')='$year-$thang' AND tinh_trang='1'";
$result=mysql_query($sql,$con);
$tong=mysql_fetch_row($result);
$tong_tien_array_com[$thang]=$tong[0];
}
return $tong_tien_array_com;
	}
?>
<?
$page = $_GET["page"];
//---------Thang 1----------------------------------------------------------------
if (isset($_POST['btnQuery']))
{
for ($i=1;$i<13;$i++)
{

$abc=tong_tien($conn,$year_box);
$bcd=tong_don_hang($conn,$year_box);
$cde=tong_don_hang_hoan_thanh($conn,$year_box);
$def=tong_tien_hoan_thanh($conn,$year_box);
?>
  
	<tr>
		<td bgcolor="<?=$color?>" class="smallfont" align="center"><?=$i?></td>
		<td bgcolor="<?=$color?>" class="smallfont" align="center"><?=$bcd[$i]?></td>
		<td bgcolor="<?=$color?>" class="smallfont" align="center"><?=number_format($abc[$i]*1000)?>&nbsp;<font color="#FF0000"><?=$currencyUnit?></font></td>
		<td bgcolor="<?=$color?>" class="smallfont" align="center"><?=number_format($cde[$i])?></td>
		<td bgcolor="<?=$color?>" class="smallfont" align="center"><?=number_format($def[$i]*1000)?>&nbsp;<font color="#FF0000"><?=$currencyUnit?></font></td>
	</tr>
<?
}
}
?>
</table>
</form>
<script language="JavaScript">
function chkallClick(o) {
  	var form = document.frmForm;
	for (var i = 0; i < form.elements.length; i++) {
		if (form.elements[i].type == "checkbox" && form.elements[i].name!="chkall") {
			form.elements[i].checked = document.frmForm.chkall.checked;
		}
	}
}
</script>
<? if($errMsg!=''){echo '<p align=center class="err">'.$errMsg.'<br></p>';}?>
<span class="smallfont">
<input type="submit" value="In dữ liệu" name="btnPrint" class="button" id="btnPrint" onclick="window.print()" />
</span>
 
<table width="100%">
  <tr><td height="10"></td></tr>
	<tr><td class="smallfont"><?='Tổng số tháng : <b>12/12</b>'?></td></tr>
</table>